﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace SPSAndong.Comm
{
	public class AutoSizeFormClass
	{
		public struct controlRect
		{
			public int Left;
			public int Top;
			public int Width;
			public int Height;
		}
		public List<AutoSizeFormClass.controlRect> oldCtrl = new List<AutoSizeFormClass.controlRect>();
		private int ctrlNo = 0;
		public void controllInitializeSize(Control mForm)
		{
			AutoSizeFormClass.controlRect item;
			item.Left = mForm.Left;
			item.Top = mForm.Top;
			item.Width = mForm.Width;
			item.Height = mForm.Height;
			this.oldCtrl.Add(item);
			this.AddControl(mForm);
		}
		private void AddControl(Control ctl)
		{
			foreach (Control control in ctl.Controls)
			{
				AutoSizeFormClass.controlRect item;
				item.Left = control.Left;
				item.Top = control.Top;
				item.Width = control.Width;
				item.Height = control.Height;
				this.oldCtrl.Add(item);
				bool flag = control.Controls.Count > 0;
				if (flag)
				{
					this.AddControl(control);
				}
			}
		}
		public void controlAutoSize(Control mForm)
		{
			bool flag = this.ctrlNo == 0;
			if (flag)
			{
				AutoSizeFormClass.controlRect item;
				item.Left = 0;
				item.Top = 0;
				item.Width = mForm.PreferredSize.Width;
				item.Height = mForm.PreferredSize.Height;
				this.oldCtrl.Add(item);
				this.AddControl(mForm);
			}
			float wScale = (float)mForm.Width / (float)this.oldCtrl[0].Width;
			float hScale = (float)mForm.Height / (float)this.oldCtrl[0].Height;
			this.ctrlNo = 1;
			this.AutoScaleControl(mForm, wScale, hScale);
		}
		private void AutoScaleControl(Control ctl, float wScale, float hScale)
		{
			foreach (Control control in ctl.Controls)
			{
				int left = this.oldCtrl[this.ctrlNo].Left;
				int top = this.oldCtrl[this.ctrlNo].Top;
				int width = this.oldCtrl[this.ctrlNo].Width;
				int height = this.oldCtrl[this.ctrlNo].Height;
				control.Left = (int)((float)left * wScale);
				control.Top = (int)((float)top * hScale);
				control.Width = (int)((float)width * wScale);
				control.Height = (int)((float)height * hScale);
				this.ctrlNo++;
				bool flag = control.Controls.Count > 0;
				if (flag)
				{
					this.AutoScaleControl(control, wScale, hScale);
				}
				bool flag2 = ctl is DataGridView;
				if (flag2)
				{
					DataGridView dataGridView = ctl as DataGridView;
					Cursor.Current = Cursors.WaitCursor;
					int num = 0;
					for (int i = 0; i < dataGridView.Columns.Count; i++)
					{
						dataGridView.AutoResizeColumn(i, DataGridViewAutoSizeColumnMode.AllCells);
						num += dataGridView.Columns[i].Width;
					}
					bool flag3 = num >= ctl.Size.Width;
					if (flag3)
					{
						dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
					}
					else
					{
						dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
					}
					Cursor.Current = Cursors.Default;
				}
			}
		}
	}
}
